/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2010
 */

package com.hibernate.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.javacommon.base.BaseEntity;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;

import cn.org.rapid_framework.util.DateConvertUtils;

/**
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
 */


@Entity
@Table(name = "AD_SITE")
public class AdSite extends BaseEntity implements java.io.Serializable{
	private static final long serialVersionUID = 5454155825314635342L;

	//alias
	public static final String TABLE_ALIAS = "广告位";
	public static final String ALIAS_ID = "id";
	public static final String ALIAS_LABEL_ID = "栏目ID";
	public static final String ALIAS_LABEL_NAME = "栏目名称";
	public static final String ALIAS_SIZE_ID = "广告位尺寸ID";
	public static final String ALIAS_SIZE = "广告位尺寸";
	public static final String ALIAS_NAME = "广告位名称";
	public static final String ALIAS_PLAY_TYPE = "展现类型";
	public static final String ALIAS_PAGE_SITE = "页面位置";
	public static final String ALIAS_SITE_CODE = "广告位标记";
	public static final String ALIAS_NETWORK_STATUS = "是否接入网络";
	public static final String ALIAS_PLAN_AMOUNT = "投放计划数";
	public static final String ALIAS_CPM_PRICE = "曝光单价";
	public static final String ALIAS_BUY_OUT_PRICE = "包断价格";
	public static final String ALIAS_CLICK_PRICE = "点击单价";
	public static final String ALIAS_REMARK = "广告位描述";
	public static final String ALIAS_DEFAULT_PLAY = "默认广告设置";
	public static final String ALIAS_HTML = "自定义HTML内容";
	public static final String ALIAS_CREATION_TIME = "添加时间";
	public static final String ALIAS_STATUS = "状态";
	public static final String ALIAS_PARALLEL_SITE = "水平位置";
	public static final String ALIAS_AD_STYLE = "广告形式";
	public static final String ALIAS_AD_DENSITY = "同屏广告密度";
	public static final String ALIAS_CLICK_CHEAT = "点击是否作弊";
	
	//date formats
	public static final String FORMAT_CREATION_TIME = DATE_FORMAT;
	

	//可以直接使用: @Length(max=50,message="用户名长度不能大于50")显示错误消息
	//columns START
	
	private Long id;
	@NotNull 
	private Long labelId;
	@NotNull 
	private Long sizeId;
	@Length(max=255)
	private String otherCode;
	@NotBlank @Length(max=70)
	private java.lang.String name;
	@NotBlank @Length(max=1)
	private java.lang.String playType;
	
	private java.lang.String playPage;
	@NotBlank @Length(max=1)
	private java.lang.String pageSite;
	@NotBlank @Length(max=50)
	private java.lang.String siteCode;
	@NotBlank @Length(max=1)
	private java.lang.String networkStatus;
	@NotNull 
	private Long planAmount;
	
	private Double cpmPrice;
	private Double buyOutPrice;
	private Double clickPrice;
	
	@Length(max=1000)
	private java.lang.String remark;
	@NotBlank @Length(max=1)
	private java.lang.String defaultPlay;
	@Length(max=500)
	private java.lang.String html;
	@NotNull 
	private java.util.Date creationTime;
	@NotBlank @Length(max=1)
	private java.lang.String status;
	
	private java.lang.String rate;
	private Long presetClicknum;
	private Long clicknum;
	private Long shownum;
	
	
	@Length(max=2)
	private java.lang.String parallelSite;
	@Length(max=50)
	private java.lang.String adStyle;
	@Length(max=2)
	private String adDensity;
	@Length(max=10)
	private java.lang.String clickCheat;
	
	private Boolean byPv;
	
	private Boolean byUv;
	
	private Boolean byUvClick;
	
	private String renderPath;
	
	private String renderClickUrl;
	
	private Integer renderFlag;
	
	private Integer renderType;
	
	private Integer optimizeFlag;	
	//columns END


	public AdSite(){
	}

	public AdSite(
		Long id
	){
		this.id = id;
	}

	

	public void setId(Long value) {
		this.id = value;
	}
	
	@Id
	@GeneratedValue(generator = "paymentableGenerator")       
	@GenericGenerator(name = "paymentableGenerator", strategy = "native") 
	@Column(name = "ID", unique = true, nullable = false, insertable = true, updatable = true, length = 20)
	public Long getId() {
		return this.id;
	}
	
	@Column(name = "LABEL_ID", unique = false, nullable = false, insertable = true, updatable = true, length = 20)
	public Long getLabelId() {
		return this.labelId;
	}
	
	public void setLabelId(Long value) {
		this.labelId = value;
	}
	
	@Column(name = "SIZE_ID", unique = false, nullable = false, insertable = true, updatable = true, length = 20)
	public Long getSizeId() {
		return this.sizeId;
	}
	
	public void setSizeId(Long value) {
		this.sizeId = value;
	}

	@Column(name = "NAME", unique = false, nullable = false, insertable = true, updatable = true, length = 50)
	public java.lang.String getName() {
		return this.name;
	}
	
	public void setName(java.lang.String value) {
		this.name = value;
	}
	
	@Column(name = "PLAY_TYPE", unique = false, nullable = false, insertable = true, updatable = true, length = 1)
	public java.lang.String getPlayType() {
		if(playType == null)
			return "1";
		return this.playType;
	}
	
    @Column(name = "OPTIMIZE_FLAG")
	public Integer getOptimizeFlag() {
		return optimizeFlag;
	}

	public void setOptimizeFlag(Integer optimizeFlag) {
		this.optimizeFlag = optimizeFlag;
	}

	public void setPlayType(java.lang.String value) {
		this.playType = value;
	}
	
	@Column(name = "PAGE_SITE", unique = false, nullable = false, insertable = true, updatable = true, length = 1)
	public java.lang.String getPageSite() {
		if(pageSite == null)
			return "1";
		return this.pageSite;
	}
	
	
	@Column(name = "CLICKNUM")
	public Long getClicknum() {
		return clicknum;
	}

	public void setClicknum(Long clicknum) {
		this.clicknum = clicknum;
	}
	@Column(name = "SHOWNUM")
	public Long getShownum() {
		return shownum;
	}

	public void setShownum(Long shownum) {
		this.shownum = shownum;
	}

	public void setPageSite(java.lang.String value) {
		this.pageSite = value;
	}
	
	@Column(name = "RENDER_TYPE")
	public Integer getRenderType() {
		return renderType;
	}

	public void setRenderType(Integer renderType) {
		this.renderType = renderType;
	}

	@Column(name = "SITE_CODE", unique = false, nullable = false, insertable = true, updatable = true, length = 50)
	public java.lang.String getSiteCode() {
		return this.siteCode;
	}
	
	public void setSiteCode(java.lang.String value) {
		this.siteCode = value;
	}
	@Column(name = "PRESET_CLICKNUM")
	public Long getPresetClicknum() {
		return presetClicknum;
	}

	public void setPresetClicknum(Long presetClicknum) {
		this.presetClicknum = presetClicknum;
	}

	@Column(name = "NETWORK_STATUS", unique = false, nullable = false, insertable = true, updatable = true, length = 1)
	public java.lang.String getNetworkStatus() {
		return this.networkStatus;
	}
	
	public void setNetworkStatus(java.lang.String value) {
		this.networkStatus = value;
	}
	
	@Column(name = "PLAN_AMOUNT", unique = false, nullable = false, insertable = true, updatable = true, length = 20)
	public Long getPlanAmount() {
		return this.planAmount;
	}
	
	public void setPlanAmount(Long value) {
		this.planAmount = value;
	}
	
	@Column(name = "CPM_PRICE", unique = false, nullable = true, insertable = true, updatable = true, length = 10)
	public Double getCpmPrice() {
		return this.cpmPrice;
	}
	
	public void setCpmPrice(Double value) {
		this.cpmPrice = value;
	}
	
	@Column(name = "BUY_OUT_PRICE", unique = false, nullable = true, insertable = true, updatable = true, length = 10)
	public Double getBuyOutPrice() {
		return this.buyOutPrice;
	}
	
	public void setBuyOutPrice(Double value) {
		this.buyOutPrice = value;
	}
	
	@Column(name = "CLICK_PRICE", unique = false, nullable = true, insertable = true, updatable = true, length = 10)
	public Double getClickPrice() {
		return this.clickPrice;
	}
	
	public void setClickPrice(Double value) {
		this.clickPrice = value;
	}
	
	@Column(name = "REMARK", unique = false, nullable = true, insertable = true, updatable = true, length = 1000)
	public java.lang.String getRemark() {
		return this.remark;
	}
	
	public void setRemark(java.lang.String value) {
		this.remark = value;
	}
	
	@Column(name = "DEFAULT_PLAY", unique = false, nullable = false, insertable = true, updatable = true, length = 1)
	public java.lang.String getDefaultPlay() {
		if(defaultPlay == null)
			return "0";
		return this.defaultPlay;
	}
	
	public void setDefaultPlay(java.lang.String value) {
		this.defaultPlay = value;
	}
	
	@Column(name = "HTML", unique = false, nullable = true, insertable = true, updatable = true, length = 500)
	public java.lang.String getHtml() {
		return this.html;
	}
	
	public void setHtml(java.lang.String value) {
		this.html = value;
	}
	
	@Column(name = "RATE")	
	public java.lang.String getRate() {
		return rate;
	}

	public void setRate(java.lang.String rate) {
		this.rate = rate;
	}

	@Transient
	public String getCreationTimeString() {
		return DateConvertUtils.format(getCreationTime(), FORMAT_CREATION_TIME);
	}
	public void setCreationTimeString(String value) {
		setCreationTime(DateConvertUtils.parse(value, FORMAT_CREATION_TIME,java.util.Date.class));
	}
	
	@Column(name = "CREATION_TIME", unique = false, nullable = false, insertable = true, updatable = true, length = 7)
	public java.util.Date getCreationTime() {
		return this.creationTime;
	}
	
	public void setCreationTime(java.util.Date value) {
		this.creationTime = value;
	}
	
	@Column(name = "STATUS", unique = false, nullable = false, insertable = true, updatable = true, length = 1)
	public java.lang.String getStatus() {
		return this.status;
	}
	
	public void setStatus(java.lang.String value) {
		this.status = value;
	}
	
	private Set<MaterialSitePlay> materialSitePlays = new HashSet<MaterialSitePlay>();
	public void setMaterialSitePlays(Set<MaterialSitePlay> materialSitePlay){
		this.materialSitePlays = materialSitePlay;
	}
	
	@OneToMany(cascade = { CascadeType.MERGE }, fetch = FetchType.LAZY, mappedBy = "adSite")
	public Set<MaterialSitePlay> getMaterialSitePlays() {
		return materialSitePlays;
	}
	
	private Label label;
	public void setLabel(Label label){
		/*if(label != null)
			label.getName();*/
		this.label = label;
	}
	
	@ManyToOne(cascade = {}, fetch = FetchType.LAZY)
	@JoinColumns({
		@JoinColumn(name = "LABEL_ID",nullable = false, insertable = false, updatable = false) 
	})
	public Label getLabel() {
		return label;
	}
	
	private AdSiteSize adSiteSize;
	public void setAdSiteSize(AdSiteSize adSiteSize){
		if(adSiteSize != null)
			adSiteSize.getHeight();
		this.adSiteSize = adSiteSize;
	}
	
	@ManyToOne(cascade = {}, fetch = FetchType.LAZY )
	@JoinColumns({
		@JoinColumn(name = "SIZE_ID",nullable = false, insertable = false, updatable = false) 
	})
	public AdSiteSize getAdSiteSize() {
		return adSiteSize;
	}
	@Column(name = "OTHER_CODE", unique = false, nullable = true, insertable = true, updatable = true, length = 255)
	public String getOtherCode() {
		return this.otherCode;
	}
	
	public void setOtherCode(String value) {
		this.otherCode = value;
	}
	
	@Column(name = "RENDER_PATH")
	public String getRenderPath() {
		return renderPath;
	}

	public void setRenderPath(String renderPath) {
		this.renderPath = renderPath;
	}

	@Column(name = "RENDER_CLICKURL")
	public String getRenderClickUrl() {
		return renderClickUrl;
	}

	public void setRenderClickUrl(String renderClickUrl) {
		this.renderClickUrl = renderClickUrl;
	}

	@Column(name = "RENDER_FLAG")
	public Integer getRenderFlag() {
		return renderFlag;
	}

	public void setRenderFlag(Integer renderFlag) {
		this.renderFlag = renderFlag;
	}

	public String toString() {
		return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
			.append("Id",getId())
			.append("LabelId",getLabelId())
			.append("SizeId",getSizeId())
			.append("Name",getName())
			.append("PlayType",getPlayType())
			.append("PageSite",getPageSite())
			.append("SiteCode",getSiteCode())
			.append("NetworkStatus",getNetworkStatus())
			.append("PlanAmount",getPlanAmount())
			.append("CpmPrice",getCpmPrice())
			.append("BuyOutPrice",getBuyOutPrice())
			.append("ClickPrice",getClickPrice())
			.append("Remark",getRemark())
			.append("DefaultPlay",getDefaultPlay())
			.append("Html",getHtml())
			.append("CreationTime",getCreationTime())
			.append("Status",getStatus())
			.toString();
	}
	
	public int hashCode() {
		return new HashCodeBuilder()
			.append(getId())
			.toHashCode();
	}
	
	public boolean equals(Object obj) {
		if(obj instanceof AdSite == false) return false;
		if(this == obj) return true;
		AdSite other = (AdSite)obj;
		return new EqualsBuilder()
			.append(getId(),other.getId())
			.isEquals();
	}
	

	@Column(name = "PARALLEL_SITE", unique = false, nullable = true, insertable = true, updatable = true, length = 2)
	public java.lang.String getParallelSite() {
		return this.parallelSite;
	}
	
	public void setParallelSite(java.lang.String value) {
		this.parallelSite = value;
	}
	
	@Column(name = "AD_STYLE", unique = false, nullable = true, insertable = true, updatable = true, length = 50)
	public java.lang.String getAdStyle() {
		return this.adStyle;
	}
	
	public void setAdStyle(java.lang.String value) {
		this.adStyle = value;
	}
	
	@Column(name = "AD_DENSITY", unique = false, nullable = true, insertable = true, updatable = true, length = 2)
	public String getAdDensity() {
		return this.adDensity;
	}
	
	public void setAdDensity(String value) {
		this.adDensity = value;
	}

	
	@Column(name = "CLICK_CHEAT", unique = false, nullable = true, insertable = true, updatable = true, length = 10)
	public java.lang.String getClickCheat() {
		return this.clickCheat;
	}
	
	public void setClickCheat(java.lang.String value) {
		this.clickCheat = value;
	}

	@Column(name = "BY_PV", nullable = true, insertable = true, updatable = true)
	public Boolean getByPv() {
		return byPv;
	}

	public void setByPv(Boolean byPv) {
		this.byPv = byPv;
	}

	@Column(name = "BY_UV", nullable = true, insertable = true, updatable = true)
	public Boolean getByUv() {
		return byUv;
	}

	public void setByUv(Boolean byUv) {
		this.byUv = byUv;
	}

	@Column(name = "BY_UV_CLICK", nullable = true, insertable = true, updatable = true)
	public Boolean getByUvClick() {
		return byUvClick;
	}

	public void setByUvClick(Boolean byUvClick) {
		this.byUvClick = byUvClick;
	}

	@Column(name = "PLAY_PAGE", nullable = true, insertable = true, updatable = true)
	public java.lang.String getPlayPage() {
		return playPage;
	}

	public void setPlayPage(java.lang.String playPage) {
		this.playPage = playPage;
	}
	
	@Transient
	public String getFullName(){
		String adSiteName = null;
		if(name.length() < 8){
			String labelName = label.getName();
			String webSite = label.getWebsite().getName();
		    //创业邦|创业学院频道|首页_创业邦浮起_300*250_浮起
			String planTypeName = null;
			switch (Integer.parseInt(playType)) {
			case 1:
				planTypeName ="固定";
				break;
			case 2:
				planTypeName ="浮起";
				break;
			case 3:
				planTypeName ="文字链";
				break;
			default:
				break;
			}
			
			adSiteName = webSite +"|"+labelName+"|"+(playPage==null?" ":playPage)+"_"+name+"_"+adSiteSize.getWidth()+"*"+adSiteSize.getHeight()+"_"+planTypeName;
		}else{
			adSiteName=name;
		}
		return adSiteName;
	}
	@Transient
	@Override
	public String getTarget() {
		// TODO Auto-generated method stub
		return "广告位";
	}
	@Transient
	@Override
	public Long getTargetId() {
		// TODO Auto-generated method stub
		return getId();
	}
	@Transient
	@Override
	public String getTargetName() {
		// TODO Auto-generated method stub
		return getName();
	}
	
}

